package com.java.al.swing;
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
  
import javax.swing.JButton;   
import javax.swing.JLabel;   
import javax.swing.JProgressBar;   
import javax.swing.SwingUtilities;   
  
public class JProgressBarTest extends javax.swing.JFrame {   
  
    {   
        // Set Look & Feel   
        try {   
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager   
                    .getCrossPlatformLookAndFeelClassName());   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    private JProgressBar proBar;   
  
    private JButton view;   
  
    private JButton hidden;   
  
    private JLabel lblMsg;   
  
    /**  
     * Auto-generated main method to display this JFrame  
     */  
    public static void main(String[] args) {   
    	JProgressBarTest inst = new JProgressBarTest();   
        inst.setVisible(true);   
    }   
  
    public JProgressBarTest() {   
        super();   
        initGUI();   
    }   
  
    private void initGUI() {   
        try {   
            getContentPane().setLayout(null);   
            this.setTitle("\u8fdb\u5ea6\u6761\u6d4b\u8bd5");   
            {   
                proBar = new JProgressBar();   
                getContentPane().add(proBar);   
                proBar.setBounds(56, 70, 245, 14);   
                proBar.setIndeterminate(true);   
                proBar.setVisible(false);   
                proBar.setStringPainted(true);   
            }   
            {   
                lblMsg = new JLabel();   
                getContentPane().add(lblMsg);   
                lblMsg.setBounds(35, 28, 91, 21);   
                lblMsg.setText("\u8fdb\u5ea6\u6761\u6d4b\u8bd5\uff1a");   
            }   
            {   
                view = new JButton();   
                getContentPane().add(view);   
                view.setText("\u663e\u793a\u8fdb\u5ea6\u6761");   
                view.setBounds(147, 28, 105, 28);   
                view.addActionListener(new ActionListener() {   
                    public void actionPerformed(ActionEvent evt) {   
                        viewActionPerformed(evt);   
                    }   
                });   
            }   
            {   
                hidden = new JButton();   
                getContentPane().add(hidden);   
                hidden.setText(" \u9690\u85cf\u8fdb\u5ea6\u6761");   
                hidden.setBounds(273, 28, 105, 28);   
                hidden.addActionListener(new ActionListener() {   
                    public void actionPerformed(ActionEvent evt) {   
                        hiddenActionPerformed(evt);   
                    }   
                });   
            }   
            pack();   
            setSize(400, 300);   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    private void viewActionPerformed(ActionEvent evt) {   
  
        proBar.setVisible(true);   
  
        try {   
            new Thread() {   
                public void run() {   
                    try {   
                        showMessage("step one...");   
                        Thread.sleep(3000);   
                        showMessage("\nstep two...");   
                        Thread.sleep(3000);   
                        showMessage("\nfinished.");   
                        Thread.sleep(3000);   
                    } catch (InterruptedException ie) {   
                        // ignored   
                    }   
                }   
            }.start();   
  
        } catch (Exception ex) {   
            ex.printStackTrace();   
        }   
  
        //如果有这句的话，进度条就不能显示   
        // proBar.setVisible(false);   
        System.out.println("执行完以上的语句在打印！！！");//立马打印的，解决不了这个问题   
    }   
  
    private void hiddenActionPerformed(ActionEvent evt) {   
  
        proBar.setVisible(false);   
    }   
  
    private void showMessage(final String msg) {   
        SwingUtilities.invokeLater(new Runnable() {   
            public void run() {   
                proBar.setString(msg);   
            }   
        });   
    }   
  
}  
